const SocketService = require('./DouYuWsApi');
const fs = require('fs')
const path = require('path')
const mainPath = path.join(__dirname, './dist/')
// 格式化消息, 非固定格式
function formatMessage(message) {
    // console.log('message:', message)
    const fields = message.split("/");
    let typeValue = null;
    let nnValue = null;
    let txtValue = null;
    fields.forEach((field) => {
        if (field.startsWith("type@=")) {
            typeValue = field.split("@=")[1];
        } else if (field.startsWith("nn@=")) {
            nnValue = field.split("@=")[1];
        } else if (field.startsWith("txt@=")) {
            txtValue = field.split("@=")[1];
        }
    });
    console.log('消息类型:', typeValue);
    if (typeValue === 'chatmsg') {
        // 弹幕消息
        // console.log('弹幕消息:', nnValue, txtValue);
        // 转成json格式
        const json = {
            type: typeValue,
            name: nnValue,
            text: txtValue
        }
        console.log('json:', json)
    }
}

let douYuWs = SocketService.Instance;
douYuWs.connect();
douYuWs.registerCallBack('chatmsg', formatMessage);

